﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BL.BetterControls
{
    [ToolboxData("<{0}:_RequiredFieldValidator runat=server></{0}:_RequiredFieldValidator>")]
    public class _RequiredFieldValidator : RequiredFieldValidator
    {
        /// <summary>
        /// För zurb så ska det vara en small
        /// </summary>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Small;
            }
        }


        /// <summary>
        /// Utökat med funktionalitet för zurb funktionalitet  
        /// </summary>
        /// <returns></returns>
        protected override bool EvaluateIsValid()
        {
            bool valid = base.EvaluateIsValid();

            Control C = this.FindControl(ControlToValidate);

            if (C is _Textbox)
            {
                ((_Textbox)C).Valid = valid;
            }

            if (valid)
            {
               CssClass = CssClass.Replace(" error", "");

               if (CssClass.Contains(" valid") == false)
               {
                   CssClass += " valid";
               }
            }
            else
            {
                CssClass = CssClass.Replace(" valid", "");

                if (CssClass.Contains(" error") == false)
                {
                    CssClass += " error";
                }
            }

            return valid;
        }

        /// <summary>
        /// plockar bort inline style och lägger till valid class om inget är validerat
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            if (CssClass.Contains(" error") == false && CssClass.Contains(" valid") == false)
            {
                CssClass += " valid";
            }
            Display = ValidatorDisplay.Dynamic;
            Style.Clear();           
            
            base.OnPreRender(e);
        }
        /// <summary>
        /// Plockar bort inline style
        /// </summary>
        /// <param name="writer"></param>
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            Display = ValidatorDisplay.Dynamic;
            Style.Clear();            
            base.RenderBeginTag(writer);

        }


  
    }
}
